"""
Case Type   : resource pool
Case Name   : 修改资源池关联的控制组，control_group入参使用单/引号，区分大小写，部分测试点合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.预置子class控制组及Workload控制组
    2.创建资源池，查询
    3.修改资源池控制组，组名使用单/双引号,查询
    4.修改资源池控制组，组名区分大小写
    5.环境清理
Expect      :
    1.创建成功
    2.创建成功
    3.修改成功
    4.WORKLOAD_A1组、low控制组不存在，修改报错
    5.清理成功
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.class_name = 'class_a'
        self.group_name = 'workload_a1'
        self.respool = ('resp_resource_pool_0037_1',
                        'resp_resource_pool_0037_2',
                        'resp_resource_pool_0037_3')

    def test_resource_pool(self):
        text = "-----step1:预置子class控制组及Workload控制组;expect:创建成功-----"
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_cgroup -c -S {self.class_name} -s 20;" \
              f"gs_cgroup -c -S {self.class_name} -G {self.group_name} -g 20" \
              f"; gs_cgroup -p"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.log.info(cmd_res)
        self.assertIn(f"CLASS  Name: {self.class_name}", cmd_res,
                      f'执行失败:{text}')
        self.assertIn(f"DEFWD  Name: {self.group_name}", cmd_res,
                      f'执行失败:{text}')

        text = "-----step2:创建资源池,查询;expect:创建成功-----"
        self.log.info(text)
        sql = f"create resource pool {self.respool[0]};" \
              f"create resource pool {self.respool[1]} " \
              f"with(control_group = '{self.class_name}:{self.group_name}');" \
              f"create resource pool {self.respool[2]} " \
              f"with(control_group = '{self.class_name}:{self.group_name}');"
        select_sql = f"select respool_name, control_group " \
                     f"from pg_resource_pool where respool_name " \
                     f"like '{self.respool[0][:-1]}%' order by 1;"
        sql_res = self.pri_sh.execut_db_sql(sql + select_sql)
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count(
            self.constant.create_resource_pool_success), f'执行失败:{text}')
        self.assertIn(
            f'{self.respool[0]} | DefaultClass:Medium\n '
            f'{self.respool[1]} | {self.class_name}:{self.group_name}\n '
            f'{self.respool[2]} | {self.class_name}:{self.group_name}\n'
            f'(3 rows)', sql_res, f'执行失败:{text}')

        text = "-----step3:修改资源池控制组，组名使用单/双引号,查询;expect:修改成功-----"
        self.log.info(text)
        sql = f'''alter resource pool {self.respool[0]} 
        with(control_group = \\"{self.class_name}:{self.group_name}\\");
        alter resource pool {self.respool[1]} 
        with(control_group = '{self.class_name}:Low');
        alter resource pool {self.respool[2]} 
        with(control_group = \\"Medium\\");'''
        sql_res = self.pri_sh.execut_db_sql(sql + select_sql)
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count(
            self.constant.alter_resource_pool_success), f'执行失败:{text}')
        self.assertIn(
            f'{self.respool[0]} | {self.class_name}:{self.group_name}\n '
            f'{self.respool[1]} | {self.class_name}:Low\n '
            f'{self.respool[2]} | DefaultClass:Medium\n'
            f'(3 rows)', sql_res, f'执行失败:{text}')

        text = "-----step4:修改资源池控制组，组名区分大小写;" \
               "expect:WORKLOAD_A1组、low控制组不存在，修改报错-----"
        self.log.info(text)
        sql = f"alter resource pool {self.respool[0]} " \
              f"with(control_group = 'low');" \
              f"alter resource pool {self.respool[2]} with(control_group = " \
              f"'{self.class_name.upper()}:{self.group_name.upper()}');"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn('ERROR:  invalid control group: low',
                      sql_res, f'执行失败:{text}')
        self.assertIn(f'ERROR:  invalid control group: '
                      f'{self.class_name.upper()}:{self.group_name.upper()}',
                      sql_res, f"执行失败:{text}")

    def tearDown(self):
        text = "-----step5:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            ';\n'.join(
                [f"drop resource pool if exists {i}" for i in self.respool]))
        self.log.info(drop_res)
        cmd = f"source {macro.DB_ENV_PATH};gs_cgroup --revert"
        self.log.info(cmd)
        cmd_res = self.pri_node.sh(cmd).result()
        self.assertEqual(3, drop_res.count(
            self.constant.drop_resource_pool_success), f'执行失败:{text}')
        self.assertEqual('', cmd_res, f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
